const Service = require('egg').Service;

class AdService extends Service {

  async find(id) {
    const ad = await this.app.mysql.get('weshop').query(sql.adinfo,[id]);
    return ad;
  }

  async insert(para) {
    return await this.app.mysql.get('weshop').insert('ad', { id: 'djp',url: para.url });
    // const result = await this.app.mysql.get('weshop').beginTransactionScope(async conn => {
    //   // don't commit or rollback by yourself
    //   const result = await conn.insert('ad', { id: '333',url: para.url });
    //   if(result.affectedRows === 1) {
    //     throw new Error('11','dddd');
    //   }
    //   return { success: true };
    // }, this.ctx);
  }



    async getNumber(){
       var p =new Promise(function(resolve,reject){
          setTimeout(function(){
              var num=Math.ceil(Math.random()*10); // 生成1-10 之间的随机数 Math.ceil(): 大于或等于给定数字的最小整数
              if(num<=5){
                 console.log('正常1111111');
                 resolve(num);
              }else{
                console.log('数字太大了想休息休息');
                 reject('数字太大了')
              }
            },2000);
        });
       return p;
    }

}

module.exports = AdService;

const sql = exports = {
  adinfo : 'select * from ad where id = ?',
};

